package prehilife;

import java.util.ArrayList;
import java.util.Iterator;

public class Mammouth extends MobileCreature {

    public boolean dead = false;
    private PrehiWorld world;
    private int nbturnInCave = 0;

    public int getNbturnInCave() {
        return nbturnInCave;
    }

    public void setNbturnInCave(int nbturnInCave) {
        this.nbturnInCave = nbturnInCave;
    }

    public Mammouth(PrehiWorld world) {
        this.world = world;
    }

    @Override
    public void evolve() {
        throw new UnsupportedOperationException();
    }

    @Override
    public void move(int deplX, int deplY) {
        int positionX = super.getPositionX();
        int positionY = super.getPositionY();
        int newposX = positionX + deplX;
        int newposY = positionY + deplY;
        if (newposX < 0) {
            newposX = world.getSizeX() + newposX;
        } else if (newposX >=world.getSizeX()) {
            newposX = newposX % world.getSizeX();
        }
        if (newposY < 0) {
            newposY = world.getSizeY() + newposY;
        } else if (newposY >= world.getSizeY()) {
            newposY = newposY % world.getSizeY();
        }

        Iterator<Creature> caseItr = world.myGameBoard[newposX][newposY].iterator();
        ArrayList<Member> memList = new ArrayList();
        while (caseItr.hasNext()) {
            Creature crt = caseItr.next();
            if (crt instanceof Cave) {
                nbturnInCave++;
                world.myGameBoard[newposX][newposY].add(this);
                world.myGameBoard[positionX][positionY].remove(this);
                super.setPosition(newposX, newposY);
                return;
            }else if (crt instanceof Member) {
                memList.add((Member) crt);
            }
            
        }
        if (memList.size() == 1) {
            memList.get(0).setHp(0);
        } else if (memList.size() > 1) {
            setDead(true);
            super.setPosition(newposX, newposY);
            return;
        }
        ArrayList<Member> memArroundList = getmemArround(newposX, newposY);
        if (memArroundList.size() == 1) {
            memArroundList.get(0).setHp(0);
        } else if (memArroundList.size() > 1) {
            setDead(true);
            super.setPosition(newposX, newposY);
            return;
        }
        world.myGameBoard[newposX][newposY].add(this);
        world.myGameBoard[positionX][positionY].remove(this);
        super.setPosition(newposX, newposY);
    }

    public void setDead(boolean dead) {
        this.dead = dead;
        if (dead == true) {
            int positionX = super.getPositionX();
            int positionY = super.getPositionY();
            world.myGameBoard[positionX][positionY].remove(this);
            world.setNbMammouths(world.getNbMammouths()-1);
        }
    }

    public boolean isDead() {
        return dead;
    }

    private ArrayList<Member> getmemArround(int posX, int posY) {
        ArrayList<Member> memList = new ArrayList();
        if ((posX - 1 >= 0 && posX - 1 < world.getSizeX()) && (posY + 1 >= 0 && posY + 1 < world.getSizeY())) {
            Iterator<Creature> caseItr = world.myGameBoard[posX - 1][posY + 1].iterator();
            while (caseItr.hasNext()) {
                Creature creature = caseItr.next();
                if (creature instanceof Member) {
                    memList.add((Member) creature);
                }
            }
        }
        if (posY + 1 >= 0 && posY + 1 < world.getSizeY()) {
            Iterator<Creature> caseItr = world.myGameBoard[posX][posY + 1].iterator();
            while (caseItr.hasNext()) {
                Creature creature = caseItr.next();
                if (creature instanceof Member) {
                    memList.add((Member) creature);
                }
            }
        }
        if ((posX + 1 >= 0 && posX + 1 < world.getSizeX()) && (posY + 1 >= 0 && posY + 1 < world.getSizeY())) {
            Iterator<Creature> caseItr = world.myGameBoard[posX + 1][posY + 1].iterator();
            while (caseItr.hasNext()) {
                Creature creature = caseItr.next();
                if (creature instanceof Member) {
                    memList.add((Member) creature);
                }
            }
        }
        if (posX - 1 >= 0 && posX - 1 < world.getSizeX()) {
            Iterator<Creature> caseItr = world.myGameBoard[posX - 1][posY].iterator();
            while (caseItr.hasNext()) {
                Creature creature = caseItr.next();
                if (creature instanceof Member) {
                    memList.add((Member) creature);
                }
            }
        }
        if (posX + 1 >= 0 && posX + 1 < world.getSizeX()) {
            Iterator<Creature> caseItr = world.myGameBoard[posX + 1][posY].iterator();
            while (caseItr.hasNext()) {
                Creature creature = caseItr.next();
                if (creature instanceof Member) {
                    memList.add((Member) creature);
                }
            }
        }
        if ((posX - 1 >= 0 && posX - 1 < world.getSizeX()) && (posY - 1 >= 0 && posY - 1 < world.getSizeY())) {
            Iterator<Creature> caseItr = world.myGameBoard[posX - 1][posY - 1].iterator();
            while (caseItr.hasNext()) {
                Creature creature = caseItr.next();
                if (creature instanceof Member) {
                    memList.add((Member) creature);
                }
            }
        }
        if (posY - 1 >= 0 && posY - 1 < world.getSizeY()) {
            Iterator<Creature> caseItr = world.myGameBoard[posX][posY - 1].iterator();
            while (caseItr.hasNext()) {
                Creature creature = caseItr.next();
                if (creature instanceof Member) {
                    memList.add((Member) creature);
                }
            }
        }
        if ((posX + 1 >= 0 && posX + 1 < world.getSizeX()) && (posY - 1 >= 0 && posY - 1 < world.getSizeY())) {
            Iterator<Creature> caseItr = world.myGameBoard[posX + 1][posY - 1].iterator();
            while (caseItr.hasNext()) {
                Creature creature = caseItr.next();
                if (creature instanceof Member) {
                    memList.add((Member) creature);
                }
            }
        }
        return memList;
    }

    @Override
    public void unevolve() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}